package com.card.game;


import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.google.common.collect.Lists;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年8月13日 下午3:15:56 
 * @version V1.0   
 */
public class HandUtils {
	public static boolean isHuPai(Vector<Card> sour) {
		Collections.sort(sour);
		return 
				isDanDui(sour) ||
				fristDuiFind(sour,0) || 
				fristDuiFind2(sour,0) 
				|| allIsDui(sour) 
				;
	}
	
	public static boolean isLsHuPai(Vector<Card> sour) {
		Collections.sort(sour);
		return 
				isDanDui(sour) ||
				fristDuiFind(sour,1) || 
				fristDuiFind2(sour,1) 
//				|| allIsDui(sour) 
				;
	}
	
	public static boolean isCanDui(Vector<Card> cards,Card outCard) {
		Vector<Card> sour = (Vector<Card>)cards.clone();
		Vector<Hand> same2 = CardUtils.findSameCardBySize(sour, 2);
		Iterator<Hand> it = same2.iterator();
		while (it.hasNext()) {
			Hand hand = it.next();
			if(outCard.equals(hand.firstElement())) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isCanMingGang(Vector<Card> cards,Card outCard) {
		Vector<Card> sour = (Vector<Card>)cards.clone();
		Vector<Hand> same3 = CardUtils.findSameCardBySize(sour, 3);
		Iterator<Hand> it = same3.iterator();
		while (it.hasNext()) {
			Hand hand = it.next();
			if(outCard.equals(hand.firstElement())) {
				return true;
			}
		}
		return false;
	}
	
	
	
	public static List<Card> getAnGang(Vector<Card> cards) {
		List<Card> ads = Lists.newArrayList();
		Vector<Card> sour = (Vector<Card>)cards.clone();
		Vector<Hand> same4 = CardUtils.findSameCardBySize(sour, 4);
		Iterator<Hand> it = same4.iterator();
		while (it.hasNext()) {
			Hand hand = it.next();
			ads.add(hand.firstElement());
		}
		return ads;
	}
	
	private static boolean firstLink(Vector<Card> cards) {
		Vector<Card> sour = (Vector<Card>)cards.clone();
		Vector<Hand> links = CardUtils.findLinkByCards(sour);
		Vector<Hand> same3 = CardUtils.findSameCardBySize(sour, 3);
		Vector<Hand> same2 = CardUtils.findSameCardBySize(sour, 2);
		return sour.isEmpty() && same2.size() == 1;
	}
	
	private static boolean firstDui(Vector<Card> cards) {
		Vector<Card> sour = (Vector<Card>)cards.clone();
		Vector<Hand> same3 = CardUtils.findSameCardBySize(sour, 3);
		Vector<Hand> same2 = CardUtils.findSameCardBySize(sour, 2);
		Vector<Hand> links = CardUtils.findLinkByCards(sour);
		return sour.isEmpty() && same2.size() == 1;
	}
	
	public static boolean isDanDui(Vector<Card> cards) {
		return cards.size() == 2 && cards.get(0).equals(cards.get(1));
	}
	public static boolean fristDuiFind(Vector<Card> cards,int minLink) {
		Vector<Card> tempSour = (Vector<Card>)cards.clone();
		Vector<Hand> same2 = CardUtils.findSameCardBySize(tempSour, 2);
		for (Hand hand : same2) {
			Vector<Card> sour2 = (Vector<Card>)cards.clone();
			for (int i = 0; i < 2; i++) {
				sour2.remove(hand.cards.get(0));
			}
			CardUtils.findSameCardBySize(sour2, 3);
			int link = CardUtils.findLinkByCards(sour2).size();
			if(link >= minLink && sour2.isEmpty()) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean fristDuiFind2(Vector<Card> cards,int minLink) {
		Vector<Card> tempSour = (Vector<Card>)cards.clone();
		Vector<Hand> same2 = CardUtils.findSameCardBySize(tempSour, 2);
		for (Hand hand : same2) {
			Vector<Card> sour2 = (Vector<Card>)cards.clone();
			for (int i = 0; i < 2; i++) {
				sour2.remove(hand.cards.get(0));
			}
			int link = CardUtils.findLinkByCards(sour2).size();
			CardUtils.findSameCardBySize(sour2, 3);
			if(link > minLink && sour2.isEmpty()) {
				return true;
			}
		}
		return false;
	}
	
	private static boolean firstSame3(Vector<Card> cards) {
		Vector<Card> sour = (Vector<Card>)cards.clone();
		Vector<Hand> same3 = CardUtils.findSameCardBySize(sour, 3);
		Vector<Hand> links = CardUtils.findLinkByCards(sour);
		Vector<Hand> same2 = CardUtils.findSameCardBySize(sour, 2);
		return sour.isEmpty() && same2.size() == 1;
	}
	
	public static boolean allIsDui(Vector<Card> cards) {
		if(cards.size() != 14) {
			return false;
		}
		Collections.sort(cards);
		Vector<Card> sour = (Vector<Card>)cards.clone();
		CardUtils.findSameCardBySize(sour, 4);
		CardUtils.findSameCardBySize(sour, 2);
		return sour.isEmpty();
	}
}
